// J2ME Compass
// Copyright (C) 2006 Dana Peters
// http://www.qcontinuum.org/compass

package org.qcontinuum.astro;

import henson.midp.Float;

public class EclipticPosition {
    
    final static private Float c0_409092 = new Float(409092, -6);
    final static private Float c0 = new Float(0);
    final static private Float c0_9174820620 = new Float(9174820620L, -10);
    final static private Float c0_3977771559 = new Float(3977771559L, -10);

    private Float mLatitude;
    private Float mLongitude;
    
    public EclipticPosition(Float latitude, Float longitude) {
        mLatitude = latitude;
        mLongitude = longitude;
    }
    
    public Float getLatitude() {
        return mLatitude;
    }
    
    public Float getLongitude() {
        return mLongitude;
    }

    public EquitorialPosition toEquitorialPosition()
    {
      // Convert to Ecliptic Polar to Cartesian
      // xg = cos(latitude) * cos(longitude);
      Float xg = Float.cos(mLatitude).Mul(Float.cos(mLongitude));
      // yg = cos(latitude) * sin(longitude);
      Float yg = Float.cos(mLatitude).Mul(Float.sin(mLongitude));
      // yg = sin(latitude);
      Float zg = Float.sin(mLatitude);

      // Rotate to Equitorial Cartesian using fixed Ecliptic angle
      Float xe = xg;
      // ye = yg * 0.9174820620 - zg * 0.3977771559;
      Float ye = yg.Mul(c0_9174820620).Sub(zg.Mul(c0_3977771559));
      // ze = yg * 0.3977771559 + zg * 0.9174820620;
      Float ze = yg.Mul(c0_3977771559).Add(zg.Mul(c0_9174820620));

      // Convert Equitorial Cartisian to Polar
      Float RA = Float.atan2(ye, xe);
      if (RA.Less(c0))
        RA = RA.Add(Float.PImul2);
      // Dec = atan2(ze, sqrt(xe * xe + ye * ye));
      Float Dec = Float.atan2(ze, Float.sqrt(xe.Mul(xe).Add(ye.Mul(ye))));
      return new EquitorialPosition(RA, Dec);
    }

}
